/**
 * 移动端适配工具函数
 */

// 检测设备类型
export const isMobile = (): boolean => {
  return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
};

export const isIOS = (): boolean => {
  return /iPhone|iPad|iPod/i.test(navigator.userAgent);
};

export const isAndroid = (): boolean => {
  return /Android/i.test(navigator.userAgent);
};

// 判断屏幕方向
export const isLandscape = (): boolean => {
  return window.innerWidth > window.innerHeight;
};

// 获取设备像素比
export const getDevicePixelRatio = (): number => {
  return window.devicePixelRatio || 1;
};

// 自适应字体大小（根据屏幕宽度动态调整）
export const adaptFontSize = (baseFontSize: number): void => {
  const screenWidth = window.innerWidth;
  const designWidth = 375; // 设计稿基准宽度，通常为iPhone 6/7/8
  const scale = screenWidth / designWidth;
  document.documentElement.style.fontSize = `${baseFontSize * scale}px`;
};

// 移动端1像素边框问题解决（处理高清屏幕下1px边框问题）
export const fixOnePxBorder = (element: HTMLElement, position: 'all' | 'top' | 'right' | 'bottom' | 'left' = 'all'): void => {
  const dpr = getDevicePixelRatio();
  if (dpr === 1) return;
  
  const positions = position === 'all' 
    ? ['top', 'right', 'bottom', 'left']
    : [position];
    
  positions.forEach(pos => {
    element.style.setProperty(
      `border-${pos}-width`,
      `${1 / dpr}px`
    );
  });
};

// 禁止页面缩放（适用于某些需要固定缩放的场景）
export const disableZoom = (): void => {
  document.addEventListener('touchstart', (event) => {
    if (event.touches.length > 1) {
      event.preventDefault();
    }
  }, { passive: false });
  
  document.addEventListener('touchmove', (event) => {
    if (event.scale !== 1) {
      event.preventDefault();
    }
  }, { passive: false });
};

// 监听屏幕方向变化
export const onOrientationChange = (callback: (isLandscape: boolean) => void): () => void => {
  const handler = () => {
    callback(isLandscape());
  };
  
  window.addEventListener('resize', handler);
  window.addEventListener('orientationchange', handler);
  
  // 返回取消监听的函数
  return () => {
    window.removeEventListener('resize', handler);
    window.removeEventListener('orientationchange', handler);
  };
};

// 获取安全区域（适用于刘海屏等特殊设备）
export interface SafeAreaInsets {
  top: number;
  right: number;
  bottom: number;
  left: number;
}

export const getSafeAreaInsets = (): SafeAreaInsets => {
  // 默认安全区域
  const defaultInsets = { top: 0, right: 0, bottom: 0, left: 0 };
  
  // 检查是否支持CSS环境变量（iOS 11.2+支持）
  if (window.CSS && window.CSS.supports && window.CSS.supports('(padding-top: env(safe-area-inset-top))')) {
    const div = document.createElement('div');
    div.style.paddingTop = 'env(safe-area-inset-top)';
    div.style.paddingRight = 'env(safe-area-inset-right)';
    div.style.paddingBottom = 'env(safe-area-inset-bottom)';
    div.style.paddingLeft = 'env(safe-area-inset-left)';
    document.body.appendChild(div);
    
    const computedStyle = window.getComputedStyle(div);
    
    const insets = {
      top: parseInt(computedStyle.paddingTop) || 0,
      right: parseInt(computedStyle.paddingRight) || 0,
      bottom: parseInt(computedStyle.paddingBottom) || 0,
      left: parseInt(computedStyle.paddingLeft) || 0
    };
    
    document.body.removeChild(div);
    return insets;
  }
  
  return defaultInsets;
}; 